<?php
/*
*	$version: 2009-5-19 12:33:39 beta 1.0
*	$id: memcache.php 
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

include ('Cache.class'.$_G['phpEx']);

final class newMemcache extends Cache
{
	private $memcacheObj = '';

	public  $memcacheSupported = false;

	private $memcacheHost = '';

	private $memcachePort = '';

	function __construct($host = '127.0.0.1',$port = '11211',$persistency = false){

		if (!extension_loaded('memcache')){ exit ('Memcache extension could not be loaded~');}
		
		$this->memcacheObj = new Memcache;
		
		if (!$persistency) 
			{ $c = @$this->memcacheObj->connect($host,$port); }
		else
			{ $c = @$this->memcacheObj->pconnect($host,$port); }
		
		if ($c)
		{
			$this->memcacheSupported = true;
			$this->memcacheHost = $host;
			$this->memcachePort = $port;
		}
	}

	public function _getCache($k){
		return ($this->memcacheSupported) ? $this->memcacheObj->get($k) : false;
	}

	public function _setCache($k , $v ,$ttl = 0){
		$val[0] = $v; $val[1] = time();
		return ($this->memcacheSupported) ? $this->memcacheObj->set($k , $val , false , $ttl) : false;
	}

	public function _delCache($k){
		return ($this->memcacheSupported) ? $this->memcacheObj->delete($k) : false;
	}

	public function _getServerStatus(){
		return ($this->memcacheSupported) ? $this->memcacheObj->getExtendedStats() : false;
	}

	public function _replaceCache($k , $v ,$ttl = 0){
		return ($this->memcacheSupported) ? $this->memcacheObj->replace($k , $v , false , $ttl) : false;
	}

	public function _touchCache($k ,$ttl = 0){
		$v = $this->_getCache($k); $val[0] = $v[0]; $val[1] = time();
		return ($this->memcacheSupported) ? $this->_replaceCache($k , $val , false , $ttl) : false;
	}
	
	public function _closeServer(){
		return ($this->memcacheSupported) ? $this->memcacheObj->close() : false;
	}
}

?>
